/**
 * 
 */
package com.zhaokuo.service.impl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhaokuo.dao.UserDao;
import com.zhaokuo.dto.SessionDTO;
import com.zhaokuo.model.User;
import com.zhaokuo.post.UserPost;
import com.zhaokuo.service.UserService;
import com.zhaokuo.util.MD5;
import com.zhaokuo.util.RegexUtil;

/**
 * @author zk 2012-3-22
 *
 */
@Service("userService")
public class UserServiceImpl implements UserService {
	@Autowired
	private UserDao userDao;

	@Override
	public void addUser(UserPost post) throws Exception {
		User u =post.convert2User(post);
		userDao.registerUser(u);
	}

	@Override
	public User checkuser(String username) {
		return userDao.selectUserByUserName(username);
		
	}

	@Override
	public User checkUserEmail(String useremail) {
		
		return userDao.selectUserByUserEmail(useremail);
	}

	@Override
	public void updateUserStatus(String user_name) throws Exception {
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		map.put("user_name", user_name);
		map.put("user_status", User.YES_ACTIVATE);
		userDao.update("updateUserSatus", map);
	}

	@Override
	public SessionDTO login(User user) {
		String user_name=user.getUser_name();
		SessionDTO dto = new SessionDTO();
		if (RegexUtil.checkEmail(user_name)) {
			user = userDao.selectUserByUserEmail(user_name);
		} else {
			user = userDao.selectUserByUserName(user_name);
		}
		dto.setUser_email(user.getUser_email());
		dto.setUser_name(user.getUser_name());
		dto.setUser_status(user.getUser_status());
		dto.setUser_id(user.getPk());
		return dto;
	}

	@Override
	public void changeUserPass(UserPost post) throws Exception {
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		map.put("user_name", post.getUser_name());
		map.put("user_pwd", MD5.MD5(post.getUser_pwd()));
		userDao.update("changePassWord",map);
	}
	

}
